package com.eastcom.bm.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.eastcom.bm.atom.RawData;
import com.eastcom.bm.dao.AbstractDAO;
import com.eastcom.bm.dao.IRawDataDao;
import com.eastcom.canicula.stope.utils.DateUtil;
import com.eastcom.common.db.JDBCTemplate;
import com.eastcom.common.db.ResultData;

public class RawDataDao extends AbstractDAO implements IRawDataDao {

	public List<RawData> getRawDatasByDataSetId(String dataSetID,Date startDate, Date endDate)throws Exception {
		// TODO Auto-generated method stub
		try{
			String strDate=startDate==null?null:DateUtil.LongToString(startDate.getTime());
			String enDate=endDate==null?null:DateUtil.LongToString(endDate.getTime());
			
            List<RawData> results=new ArrayList<RawData>();
			
			StringBuffer sql=new StringBuffer("select * from qa_raw_data ");
			boolean firstCondition=this.appendNotNullDateAfterCondition(sql, "TIME_STAMP", strDate, true);
			this.appendNotNullDateBeforeCondition(sql, "TIME_STAMP", enDate, firstCondition);
			
			ResultData rd=JDBCTemplate.executeSQLQuery(sql.toString(), JDBCTemplate.PROPAGATION_NEVER, false, false);
			while(rd.next()){
				RawData rawData=new RawData();
				rawData.setBaselineDown(rd.getDouble(8));
				rawData.setBaselineUp(rd.getDouble(7));
				rawData.setTimeStamp(rd.getDate(1));
				rawData.setDatasetId(rd.getString(2));
				rawData.setNeId(rd.getString(3));
				rawData.setKpiId(rd.getString(4));
				rawData.setValue(rd.getDouble(6));
				
				results.add(rawData);
			}

			return results;
		}
		catch(Exception e){
			
			e.printStackTrace();
			throw e;
		}
	}

}
